package com.gopay.bis.ofpay.operation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.ofpay.operation.transformer.TransformerCharge;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.util.HttpTransport;

public class OfpayChargeHelper {
    private final static Logger logger = LoggerFactory.getLogger(OfpayChargeHelper.class);

    private HttpTransport httpTransport;
    private TransformerCharge transformer;
    private String notifyUrl;
    
    public TpasMessage mobileCharge(TpasMessage base) throws Exception {
        logger.info("ofpay mobile charge req:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        MobileMessage reqMsg = (MobileMessage)base;
        Map<String, String> param = new HashMap<String, String>();
        setBaseInfo(reqMsg, param);
        param.put("cardid", reqMsg.getCardId());
        param.put("cardnum", reqMsg.getCardNum());
//        if(StringUtils.isNotEmpty(reqMsg.getMcType()))
        param.put("mctype", reqMsg.getMcType());
        param.put("sporder_id", reqMsg.getBankOrderId());
        param.put("sporder_time", reqMsg.getOrderDate());
        param.put("game_userid", reqMsg.getOuterCustID());
        param.put("ret_url", notifyUrl);
        param.put("md5_str", reqMsg.getPlainMessage().toUpperCase());
        httpTransport.setUrl(reqMsg.getPayUrl());
        System.out.println(param);
        String resXml = httpTransport.submit(param);
//        String resXml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?> <orderinfo><err_msg></err_msg><retcode>1</retcode>"
//        +"<orderid>S1511021552911</orderid><cardid>141209</cardid><cardnum>1</cardnum><ordercash>1.54</ordercash>"
//        +"<cardname>北京移动话费1元直充</cardname><sporder_id>20151102000002mobile</sporder_id><game_userid>13522441895</game_userid><game_state>0</game_state></orderinfo>";
        MobileMessage result = transformer.parseMobileCharge(resXml);
        return result;
    }

    public TpasMessage flowCharge(TpasMessage base) throws Exception {
        logger.info("ofpay flow charge req:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        FlowMessage reqMsg = (FlowMessage)base;
        Map<String, String> param = new HashMap<String, String>();
        setBaseInfo(reqMsg, param);
        param.put("phoneno", reqMsg.getOuterCustID());
        param.put("perValue", reqMsg.getPerValue());
        param.put("flowValue", reqMsg.getFlowValue());
        param.put("range", reqMsg.getRange());
        param.put("effectStartTime", reqMsg.getEffectStartTime());
        param.put("effectTime", reqMsg.getEffectTime());
        param.put("netType", reqMsg.getNetType());
        param.put("sporderId", reqMsg.getBankOrderId());
        param.put("effectTime", reqMsg.getEffectTime());
        param.put("md5Str", reqMsg.getPlainMessage().toUpperCase());
        param.put("retUrl", notifyUrl);
        System.out.println(param);
        httpTransport.setUrl(reqMsg.getPayUrl());
        String resXml = httpTransport.submit(param);
//        String resXml = "<orderinfo><err_msg></err_msg><retcode>1</retcode><orderid>S1511021886326</orderid><cardid>12827</cardid>"
//        	+"<cardnum>1</cardnum><ordercash>1</ordercash><cardname>北京移动1元2M全国当日生效当月有效4G流量包</cardname><sporder_id>20151102000001flow</sporder_id>"
//        	+"<game_userid>13522441895</game_userid><game_state>0</game_state></orderinfo>";
        FlowMessage result = transformer.parseFlowCharge(resXml);
        return result;
    }
    
    public TpasMessage oilCardCharge(TpasMessage base) throws Exception {
        logger.info("ofpay oilCard charge req:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        OilCardMessage reqMsg = (OilCardMessage)base;
        Map<String, String> param = new HashMap<String, String>();
        setBaseInfo(reqMsg, param);
        param.put("cardid", reqMsg.getCardId());
        param.put("cardnum", reqMsg.getCardNum());
        param.put("sporder_id", reqMsg.getBankOrderId());
        param.put("sporder_time", reqMsg.getOrderDate());
        param.put("game_userid", reqMsg.getOuterCustID());
        param.put("chargeType", reqMsg.getCardType());
        param.put("gasCardTel", reqMsg.getHolderMobile());
        param.put("gasCardName", reqMsg.getUsername());
        param.put("invoiceFlag", "0");
        param.put("md5_str", reqMsg.getPlainMessage().toUpperCase());
        param.put("ret_url", notifyUrl);
System.out.println(param);
        httpTransport.setUrl(reqMsg.getPayUrl());
        String resXml = httpTransport.submit(param);
//        String resXml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?><orderinfo><err_msg></err_msg><retcode>1</retcode>"
//        +"<orderid>S1511022771490</orderid><cardid>64127500</cardid><cardnum>1</cardnum><ordercash>1.003</ordercash>"
//        +"<cardname>全国中石化加油任意充直充</cardname><sporder_id>20151102000001oilcard</sporder_id><game_userid>1000113200013667017</game_userid><game_state>0</game_state></orderinfo>";
        MobileMessage result = transformer.parseMobileCharge(resXml);
        return result;
    }

    private void setBaseInfo(TpasMessage reqMsg, Map<String, String> param) {
        param.put("userid", reqMsg.getMerID());
        param.put("userpws", reqMsg.getPrivatePwd());
        param.put("version", reqMsg.getVersion());
    }

    public void setHttpTransport(HttpTransport httpTransport) {
        this.httpTransport = httpTransport;
    }
    public void setTransformer(TransformerCharge transformer) {
        this.transformer = transformer;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

}
